/**
 * Content required at start page
 */

dojo.require("dijit.form.Select");
dojo.require("dojo.parser");

var register_visible = false;
function fade_register() {
	if (register_visible == false) {
		dojo.byId("login").style.display = "block";
		dojo.fadeIn({
			node : "login"
		}).play();
		dojo.byId("loginBtn").innerHTML = Galaxytool_INDEX_CLOSE;
		register_visible = true;
	} else {
		dojo.fadeOut({
			node : "login"
		}).play();
		dojo.byId("loginBtn").innerHTML = Galaxytool_L_REGISTER;
		dojo.byId("login").style.display = "none";
		register_visible = false;
	}

}


function show(name) {
	dojo.byId("mainContent").style.display  = "none";
	dojo.byId("messageArea").style.display  = "none";
	dojo.byId("passwordLost").style.display = "none";	
	dojo.byId("loadingArea").style.display  = "none";	
	
	if (name == "mainContent")  dojo.byId("mainContent").style.display   = "block";
	if (name == "messageArea")  dojo.byId("messageArea").style.display   = "block";
	if (name == "passwordLost") dojo.byId("passwordLost").style.display  = "block";
	if (name == "loadingArea")  dojo.byId("loadingArea").style.display  = "block";
}

function show_lost_password() {
	show("passwordLost");
}

function show_main_content() {
	show("mainContent");
}

function reset_password() {
	// change to "loading" to avoid further clicks
	show("loadingArea");
	var xhrArgs = {
        url: "secret/ajax/ajax_startpage.php?type=reset",
        form: dojo.byId("pwLostForm"),
        handleAs: "json",
        load: function(jsonData) {
        	var content = getMessagesHTML(jsonData,"images");
        	dojo.byId("messageAreaContent").innerHTML = content;
        	show("messageArea");
        },
        error: function(error) {
			console.log("Error:");
			console.log(error);
        }
    }	
	dojo.xhrPost(xhrArgs);
}

function validate_and_submit() {
	var error = false;
	
	if (dojo.byId("loginusername").value == "")  { error = true; dojo.addClass("loginusername", "formError"); } else { dojo.removeClass("loginusername", "formError");	}
	if (dojo.byId("password1").value == "")      { error = true; dojo.addClass("password1", "formError"); } else { dojo.removeClass("password1", "formError");	}
	if (dojo.byId("password2").value == "")      { error = true; dojo.addClass("password2", "formError"); } else { dojo.removeClass("password2", "formError");	}
	if (dojo.byId("email").value == "")          { error = true; dojo.addClass("email", "formError"); } else { dojo.removeClass("email", "formError");	}
	
	// both values are not needed anymore
	//if (dojo.byId("ingameusername").value == "") { error = true; dojo.addClass("ingameusername", "formError"); } else { dojo.removeClass("ingameusername", "formError");	}
	//if (dojo.byId("allytag").value == "")        { error = true; dojo.addClass("allytag", "formError"); } else { dojo.removeClass("allytag", "formError");	}
	
	if (error) return false;
	
	// passwords equal ?
	if (dojo.byId("password1").value != dojo.byId("password2").value) {
		dojo.addClass("password1", "formError");
		dojo.addClass("password2", "formError");
		error = true;
	} 
	
	// email valid?
	if (validateEmail(dojo.byId("email").value) == false) {
		dojo.addClass("email", "formError");
		error = true;
	}
	
	if (error) return false;
	
	document.forms['subscribeForm'].submit();
}

function check_password_strength(){
	var value = dojo.trim(dojo.byId("password1").value);
	// remove password classes
	dojo.removeClass("password_strength", "password_strength_weak");
	dojo.removeClass("password_strength", "password_strength_good");
	dojo.removeClass("password_strength", "password_strength_strong");
	dojo.removeClass("password_strength", "password_strength_very_strong");
	dojo.byId("password_strength").title = "";
	if(value != "") {
		if (value.length > 5) {
			var pws = 0;

			if (value.match(/\d{1,}/))	 		   {pws++;}
			if (value.match(/[a-zäöü]{1,}/))	   {pws++;}
			if (value.match(/[A-ZÄÜÖ]{1,}/))	   {pws++;}
			if (value.match(/\W/)) 				   {pws++;}

			if((value.length > 7) && (pws > 3)) {
				dojo.addClass("password_strength", "password_strength_very_strong"); 
				dojo.byId("password_strength").title = register_vstrong;
			} else if (pws > 2) {
				dojo.addClass("password_strength", "password_strength_strong");
				dojo.byId("password_strength").title = register_strong;
			} else if (pws > 1) {
				dojo.addClass("password_strength", "password_strength_good");
				dojo.byId("password_strength").title = register_good;
			} else {
				dojo.addClass("password_strength", "password_strength_weak");
				dojo.byId("password_strength").title = register_weak;
			}
		} else {
			dojo.addClass("password_strength", "password_strength_weak");
			dojo.byId("password_strength").title = register_weak;
		}
	}
}
function startpage_init() {
	var passwordNode = dojo.byId("password1");
	dojo.connect(passwordNode,"onchange",check_password_strength);
	dojo.connect(passwordNode,"onkeyup",check_password_strength);
}

dojo.ready(startpage_init);
